package com.ymatou.iapi.productimporter.testcase;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productimporter.parameter.CreateBean;
import com.ymatou.iapi.productimporter.parameter.CreateTaskPacketDto;
import com.ymatou.iapi.productimporter.service.CreateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.ProductImportTask;
import com.ymttest.database.model.ProductPacket;
import com.ymttest.database.sqlwapper.ProductImporterWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*创建导入任务
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Create{
	private static CreateBean createBean;
	private static CreateCall createCall;
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建导入任务");
	}

	@Before
	public void caseUp() {
		createBean=new CreateBean();
		createCall=new CreateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(CreateBean createBean){
		int taskId = createCall.getJsonObject().get("data").getAsJsonObject().get("taskId").getAsInt();
		ProductImporterWapper productImporterWapper = new ProductImporterWapper();
		ProductImportTask productImportTask = productImporterWapper.selectProductImportTaskByTaskId(taskId);
		Logger.verifyEquals(createBean.getSellerId(),productImportTask.getSellerId(),"验证sellerId");
		Logger.verifyEquals(createBean.getFileName(),productImportTask.getLocalFilename(),"验证FileName");
		Logger.verifyEquals(createBean.getFileSign(),productImportTask.getFileSign(),"验证fileSign");
		if(createBean.getProductSource()==0) {
			//兼容默认1淘宝
			Logger.verifyEquals(1,productImportTask.getProductSource(),"验证ProductSource");
		}else {
			Logger.verifyEquals(createBean.getProductSource(),productImportTask.getProductSource(),"验证ProductSource");
		}
		List<ProductPacket> productPackets = productImporterWapper.selectProductPacketByTaskId(taskId);
		Logger.verifyEquals(createBean.getPacketList().size(),productPackets.size(),"验证导入商品数据包列表数量一致");
		productPackets.forEach(p->{
			createBean.getPacketList().forEach(x->{
				if (p.getPacketNo()==x.getPacketNo()){
					Logger.verifyEquals(p.getPacketUrl(),x.getPacketUrl(),"验证PacketUrl");
				}
			});
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_01() {
		Logger.start(true,"创建导入任务,单数据包");
		try {
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试"+ TestDataManager.getRandomNum(5));
			createBean.setFileSign("import"+TestDataManager.getRandomNum(6));
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
			createTaskPacketDto.setPacketUrl("http://111");
			createBean.setPacketList(Arrays.asList(createTaskPacketDto));
			createCall.setData(createBean);
			createCall.callService();			
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_02() {
		Logger.start(true,"创建导入任务,多数据包");
		try {
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试"+ TestDataManager.getRandomNum(5));
			createBean.setFileSign("import"+TestDataManager.getRandomNum(6));
			List<CreateTaskPacketDto> packetList = new ArrayList<>();
			for (int i = 0;i<3;i++){
				CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
				createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
				createTaskPacketDto.setPacketUrl("http://111");
				packetList.add(createTaskPacketDto);
			}
			createBean.setPacketList(packetList);
			createCall.setData(createBean);
			createCall.callService();
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_03() {
		Logger.start(true,"创建导入任务,数据包序号有重复,创建失败");
		try {
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试"+ TestDataManager.getRandomNum(5));
			createBean.setFileSign("import"+TestDataManager.getRandomNum(6));
			List<CreateTaskPacketDto> packetList = new ArrayList<>();
			for (int i = 0;i<3;i++){
				CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
				createTaskPacketDto.setPacketNo(1);
				createTaskPacketDto.setPacketUrl("http://111");
				packetList.add(createTaskPacketDto);
			}
			createBean.setPacketList(packetList);
			createCall.setData(createBean);
			createCall.callService();
			Logger.verifyEquals("false",createCall.getString("success"),"验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_04() {
		Logger.start(true,"创建导入任务,上传了重复的文件,创建失败");
		try {
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试"+ TestDataManager.getRandomNum(5));
			String fileSign = "import"+TestDataManager.getRandomNum(6);
			createBean.setFileSign(fileSign);
			List<CreateTaskPacketDto> packetList = new ArrayList<>();
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
			createTaskPacketDto.setPacketUrl("http://111");
			packetList.add(createTaskPacketDto);
			createBean.setPacketList(packetList);
			createCall.setData(createBean);
			createCall.callService();
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			verifyDB(createBean);
			createCall.callService();
			Logger.verifyEquals("false",createCall.getString("success"),"验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_05() {
		Logger.start(true,"创建导入任务,商品数据包列表,创建失败");
		try {
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试"+ TestDataManager.getRandomNum(5));
			String fileSign = "import"+TestDataManager.getRandomNum(6);
			createBean.setFileSign(fileSign);
			List<CreateTaskPacketDto> packetList = new ArrayList<>();

			createBean.setPacketList(packetList);
			createCall.setData(createBean);
			createCall.callService();
			Logger.verifyEquals("false",createCall.getString("success"),"验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_06() {
		Logger.start(true,"创建导入任务,单数据包-YMT导入");
		try {
			
			Map<String, Object> m = ProductManagerCallServiceV3.CreateDefYmtExportFile();
			String PacketUrl=m.get("file").toString();
			String FileSign=m.get("md5").toString();
			createBean.setAppId(TestDataManager.getRandomNum(5));
			createBean.setSellerId(vipRootUserId);
			createBean.setFileName("测试YMT"+ TestDataManager.getRandomNum(5));
			createBean.setFileSign(FileSign);
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
			createTaskPacketDto.setPacketUrl(PacketUrl);
			createBean.setPacketList(Arrays.asList(createTaskPacketDto));
			createBean.setProductSource(2);
			createCall.setData(createBean);
			createCall.callService();			
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_07() {
		Logger.start(true,"创建导入任务，excel-YMT导入");
		try {
			int rootUserId = 3383;
			//导入一个excel商品
			createBean.setSellerId(rootUserId);
			createBean.setFileName("test.xlsx");
			createBean.setFileSign("4872b9c5721e5874"+TestDataManager.getRandomNum(10));
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
//			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/16/05/rBBlD16n-ZWAKemtAABfyX9FijM26.xlsx");
//			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/16/05/rBBlD16n9fKAAt8gAAAzNcpVR7g53.xlsx");
			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/16/14/rBBlD16o6gCAJ_7GAAAzN85nYu865.xlsx");
			createBean.setPacketList(Arrays.asList(createTaskPacketDto));
			createBean.setTaskType(2);
			createBean.setProductSource(2);
			createCall.setData(createBean);
			createCall.callService();
			
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			int taskId = createCall.getJsonObject().getAsJsonObject("data").get("taskId").getAsInt();
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_08() {
		Logger.start(true,"创建导入任务，新增条形码");
		try {
			int rootUserId = 3383;
			//导入一个excel商品
			createBean.setSellerId(rootUserId);
			createBean.setFileName("test.xlsx");
			createBean.setFileSign("4872b9c5721e5874"+TestDataManager.getRandomNum(10));
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
//			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/17/8C/rBBlD17GWNKAVuhcAAbrSbsU2OM01.xlsx");
//			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/17/8C/rBBlD17GWjGAGqc2AAAzpHveGyc07.xlsx");
			createTaskPacketDto.setPacketUrl("http://file.iapi.ymatou.com/G01/M00/17/DC/rBBlD17QzsOAOZhkAAArfchmUj039.xlsx");
			createBean.setPacketList(Arrays.asList(createTaskPacketDto));
			createBean.setTaskType(2);
			createBean.setProductSource(2);
			createCall.setData(createBean);
			createCall.callService();
			
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			int taskId = createCall.getJsonObject().getAsJsonObject("data").get("taskId").getAsInt();
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Create_09() {
		Logger.start(true,"创建导入任务，新增仓库");
		try {
			int rootUserId = 3383;
			//导入一个excel商品
			createBean.setSellerId(rootUserId);
			createBean.setFileName("test.xlsx");
			createBean.setFileSign("4872b9c5721e5874"+TestDataManager.getRandomNum(10));
			CreateTaskPacketDto createTaskPacketDto = new CreateTaskPacketDto();
			createTaskPacketDto.setPacketNo(Integer.parseInt(TestDataManager.getRandomNum(6)));
			File file = new File(System.getProperty("user.home") + "\\Desktop\\" + "test.xlsx");
			String uploadFile = TestDataManager.UploadFile(file.getPath());
			createTaskPacketDto.setPacketUrl(uploadFile);
			createBean.setPacketList(Arrays.asList(createTaskPacketDto));
			createBean.setTaskType(2);
			createBean.setProductSource(2);
			createCall.setData(createBean);
			createCall.callService();
			
			Logger.verifyEquals("true",createCall.getString("success"),"验证success");
			int taskId = createCall.getJsonObject().getAsJsonObject("data").get("taskId").getAsInt();
			verifyDB(createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}